package ltd.newbee.mall.controller.mall;

import ltd.newbee.mall.common.Constants;
import ltd.newbee.mall.controller.vo.UserVO;
import ltd.newbee.mall.entity.Evaluation;
import ltd.newbee.mall.service.EvaluationService;
import ltd.newbee.mall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/evaluation")
public class EvaluationController {

    @Autowired
    private EvaluationService evaluationService;

    @Autowired
    private OrderService orderService;

    @RequestMapping("/evaluate")
    public String evaluate(@RequestBody Evaluation evaluation, HttpSession httpSession){
       try{
           UserVO user = (UserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
           evaluation.setCreatTime(new Date());
           evaluation.setUserId(user.getUserId());
           if(user!= null){
               evaluation.setUserName(StringUtils.isEmpty(user.getNickName())?user.getLoginName():user.getNickName());
           }
           evaluationService.save(evaluation);
           orderService.updateOrderItem(evaluation.getOrderId());
           return "0";
       }catch (Exception e){
           e.printStackTrace();
           return "-1";
       }
    }

}
